package com.yunze.system.service.impl.yunze.fhgl;

import com.yunze.common.mapper.mysql.fhgl.FhglExpressInquiryDetailsMapper;
import com.yunze.common.mapper.mysql.fhgl.FhglExpressInquiryMapper;
import com.yunze.common.mapper.mysql.fhgl.Ship.*;
import com.yunze.common.utils.yunze.ID_Generate;
import com.yunze.common.utils.yunze.PageUtil;
import com.yunze.system.service.yunze.fhgl.IFhglExpressInquiryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class FhglExpressInquiryServiceImpl implements IFhglExpressInquiryService {
    @Resource
    private FhglShippingApplicationMapper fhglShippingApplicationMapper;
    @Resource
    private FhglShippingApplicationFormalDetailsMapper fhglShippingApplicationFormalDetailsMapper;
    @Resource
    private FhglShippingApplicationInvoiceMapper fhglShippingApplicationInvoiceMapper;
    @Resource
    private FhglShippingApplicationOrdinaryMapper fhglShippingApplicationOrdinaryMapper;
    @Resource
    private FhglShippingApplicationOtherMapper fhglShippingApplicationOtherMapper;
    @Resource
    private FhglExpressInquiryMapper fhglExpressInquiryMapper;
    @Resource
    private FhglExpressInquiryDetailsMapper fhglExpressInquiryDetailsMapper;



    @Override
    public Map<String, Object> selMap(Map<String, Object> map) {
        Map<String, Object> Rmp=new HashMap<String, Object>();
        Integer currenPage=map.get("pageNum")!=null?Integer.parseInt(map.get("pageNum").toString()):0;
        Integer pageSize=map.get("pageSize")!=null?Integer.parseInt(map.get("pageSize").toString()):10;
        Integer rowCount = fhglExpressInquiryMapper.selMapCount(map);
        rowCount=rowCount!=null?rowCount:0;
        PageUtil pu=new PageUtil(rowCount,currenPage , pageSize);//初始化分页工具类
        //数据打包
        map.put("StarRow", pu.getStarRow());
        map.put("PageSize", pu.getPageSize());
        Rmp.put("Pu", pu);
        Rmp.put("Data", fhglExpressInquiryMapper.selMap(map));
        return  Rmp;
    }

    @Override
    public boolean upd_EIY(Map<String, Object> map) {
        return (fhglExpressInquiryMapper.upd_EIY(map)>0);
    }

    @Override
    @Transactional
    public boolean add_EIY(Map<String, Object> map) {
        boolean bool = false,upd_bool= false,add_bool= false;
        Map<String, Object> EIY_Map=(Map<String, Object>) map.get("EIY_Map");
        Integer EIY_ID_CT = fhglExpressInquiryMapper.sel_data_count();//查询今日注册供应商数量
        EIY_ID_CT=EIY_ID_CT!=null?EIY_ID_CT:0;//如果今日添加为0会反回null,给个默认值0
        String EIY_ID=ID_Generate.G_id("FEIY", EIY_ID_CT, 4);//生成ID
        EIY_Map.put("EIY_ID", EIY_ID);//赋值id
        //System.out.println("EIY_Map   ="+EIY_Map);
        add_bool= (fhglExpressInquiryMapper.add_EIY(EIY_Map)>0);//添加快递公司信息
        map.put("EIY_ID", EIY_ID);//赋值id

        fhglExpressInquiryDetailsMapper.add_EIDS(map);//新增快递详情


        List<Map<String, Object>> ASOY_arr= (List<Map<String, Object>>) map.get("ASOY_arr");
        List<Map<String, Object>> SAFDS_arr= (List<Map<String, Object>>) map.get("SAFDS_arr");

        String Outbound_status = "0";
        Map<String, Object> SAN_Map=(Map<String, Object>) map.get("SAN_Map");
        if(ASOY_arr.size()>0 || SAFDS_arr.size()>0){
            Outbound_status = "1";
        }
        SAN_Map.put("Outbound_status",Outbound_status);
        upd_bool=(fhglShippingApplicationMapper.upd_SAN(SAN_Map)>0);//修改发货申请
        //修改发货商品信息
        //普通发货判断
        if(ASOY_arr.size()>0) {
            for (int i = 0; i < ASOY_arr.size(); i++) {
                Map<String, Object> ASOY_Map = ASOY_arr.get(i);
                fhglShippingApplicationOrdinaryMapper.upd_date(ASOY_Map);
            }
        }
        //发票发货信息判断
        List<Map<String, Object>> SAIE_arr= (List<Map<String, Object>>) map.get("SAIE_arr");
        if(SAIE_arr.size()>0) {
            for (int i = 0; i < SAIE_arr.size(); i++) {
                Map<String, Object> SAIE_Map = SAIE_arr.get(i);
                fhglShippingApplicationInvoiceMapper.upd_date(SAIE_Map);
            }
        }
        //其他发货信息判断
        List<Map<String, Object>> SAOR_arr= (List<Map<String, Object>>) map.get("SAOR_arr");
        if(SAOR_arr.size()>0) {
            for (int i = 0; i < SAOR_arr.size(); i++) {
                Map<String, Object> SAOR_Map = SAOR_arr.get(i);
                fhglShippingApplicationOtherMapper.upd_date(SAOR_Map);
            }
        }
        
        //正式合同发货信息判断
        if(SAFDS_arr.size()>0) {
            for (int i = 0; i < SAFDS_arr.size(); i++) {
                Map<String, Object> SAFDS_Map = SAFDS_arr.get(i);
                fhglShippingApplicationFormalDetailsMapper.upd_date(SAFDS_Map);
            }
        }
        if(add_bool==true && upd_bool==true) {
            bool=true;
        }
        return bool;

    }

    @Override
    public Map<String, Object> find_data(Map<String, Object> map) {
        Map<String, Object> mmp =new HashMap<String, Object>();
        mmp.put("EIY_Map", fhglExpressInquiryMapper.find_data(map));
        mmp.put("EIDS_arr", fhglExpressInquiryDetailsMapper.find_data(map));
        return mmp;
    }


    @Override
    public boolean upd_EIY_data(Map<String, Object> map) {
        boolean bool=false,
                upd_bool=false;
        Map<String, Object> EIY_Map=(Map<String, Object>) map.get("EIY_Map");
        upd_bool=(fhglExpressInquiryMapper.upd_EIY_data(EIY_Map)>0);//修改 快递查询信息
        /*dao.delete("fhglExpressInquiryDetailsMapper.del_EIDS",map);//删除 快递查询信息详情
         */		/*if(((List<Map<String, Object>>)map.get("EIDS_arr")).size()!=0) {
			add_bool=(((Integer)dao.save("fhglExpressInquiryDetailsMapper.add_EIDS",map))>0);//新增 快递查询信息
		}else {add_bool=true;}*/

        if(upd_bool==true) {
            bool=true;
        }
        return bool;
    }


    @Override
    public boolean del_EIY(Map<String, Object> map) {
        fhglExpressInquiryDetailsMapper.del_EIDS(map);//删除 快递公司详情
        return (fhglExpressInquiryMapper.del_EIY(map)>0);
    }

    @Override
    public List<String> find_ID(Map<String, Object> map) {
        return fhglExpressInquiryMapper.find_ID( map);
    }

    @Override
    public Map<String, Object> Imput_data(Map<String, Object> map) {
        Map<String, Object> R_map = new HashMap<String, Object>();
        //获取  出库需导入的商品 '销售合同','临时合同' 商品名称
        List<String> name_arr = fhglExpressInquiryDetailsMapper.find_Name_arr(map);
        //首先 拿着 快递 名称 去 销售价格表 中 找 对应名称的 数据
        if(name_arr!=null && name_arr.size()>0) {
            map.put("name_arr", name_arr);
            List<Map<String, Object>> Spe_arr = fhglExpressInquiryDetailsMapper.Imput_Spe(map);

            //销售价格表 数据
            R_map.put("Spe_arr", Spe_arr);
            List<String> PT_name_arr = new ArrayList<String>();//用来储存 ，在销售价格表中 未找到的 商品名称
            if(Spe_arr !=null && Spe_arr.size()>0) {
                //循环 判断 出库需导入的商品 名称 是否在 反回的 销售价格表中
                for (int i = 0; i < name_arr.size(); i++) {
                    boolean bool = false;
                    String name =  name_arr.get(i);
                    for (int j = 0; j < Spe_arr.size(); j++) {
                        Map<String, Object> O_map =Spe_arr.get(j);
                        //快递名称 在 销售价格 表 中 的 名称， 不用 在 添加 到 普通 发货 中
                        if(name.equals(O_map.get("Spe_Sales_Name").toString())) {
                            bool =true;
                            break;//结束本次循环
                        }
                    }
                    //未找到 时 将快递名称 添加至等待加载 的商品发货中
                    if(bool==false) {PT_name_arr.add(name);}
                }
            }
            //待 查询商品名称 数组 不为空时获取商品信息
            if(PT_name_arr.size()>0) {
                map.put("type", "2");
                map.put("PT_name_arr", PT_name_arr);
                R_map.put("PT_arr", fhglExpressInquiryDetailsMapper.Imput_PT(map));
            }
        }
        //赋值  普通发货  数据
        map.put("type", "1");
        R_map.put("PT_data", fhglExpressInquiryDetailsMapper.Imput_PT(map));
        return R_map;
    }



    



}
















